package org.shopen.ai.puremvc.view
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.view.common.HeaderControlBarMediator;
	import org.shopen.ai.puremvc.view.common.IntroWindowMediator;
	import org.shopen.ai.puremvc.view.project.ProjectWindowMediator;

	public class AiMediator extends Mediator
	{
		public static const NAME : String = "AiMediator";
		
		public function AiMediator(viewComponent:Ai=null)
		{
			super(AiMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			facade.registerMediator(new HeaderControlBarMediator(application.headerControlBar));
			addEventListeners();
		}
		
		override public function getMediatorName():String
		{
			return AiMediator.NAME;
		}
		
		protected virtual function get application() : Ai
		{
			var result : Ai = viewComponent as Ai;
			return result;
		}
		
		override public function listNotificationInterests():Array
		{
			return [StateMachine.CHANGED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var noteName : String = notification.getName();
			switch (noteName)
			{
				case StateMachine.CHANGED:
					handleStateChanged(notification);
					break;
			}
		}
		
		protected function handleStateChanged(notification : INotification) : void
        {
        	var type : String = notification.getType();
        	switch (type)
        	{
        		case ApplicationConstants.STATE_INTRO:
        			application.viewState = Ai.VIEWSTATE_INTRO;
        			break;
        		case ApplicationConstants.STATE_PROJECT:
        			application.viewState = Ai.VIEWSTATE_PROJECT;         	
        			break;
        	}
        }
		
		private function addEventListeners() : void
		{
			application.addEventListener(Ai.INTRO_WINDOW_CREATED, onIntroWindowCreated);
			application.addEventListener(Ai.PROJECT_WINDOW_CREATED, onProjectWindowCreated);
		}
		
		private function onIntroWindowCreated(event : Event) : void
		{
			checkForMediator(Ai.INTROWINDOW, application.introWindow);
		}
		
		private function onProjectWindowCreated(event : Event) : void
		{
			checkForMediator(Ai.PROJECTWINDOW, application.projectWindow);
		}
		
		protected function checkForMediator(childSelector : Number, child : Object):void
        {
            switch (childSelector)
            {
                 case Ai.INTROWINDOW:
                      if (!facade.hasMediator(IntroWindowMediator.NAME))
                         facade.registerMediator(new IntroWindowMediator(child));
                      break;
                 case Ai.PROJECTWINDOW:
                 	  if (!facade.hasMediator(ProjectWindowMediator.NAME))
                      	facade.registerMediator(new ProjectWindowMediator(child));
                 	  break;         
            }
        }
	}
}